/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.hagopa.games.moneypool.msgs.ContentTypes.game;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import org.hagopa.games.moneypool.game.ErrorsEnum;
import org.hagopa.games.moneypool.msgs.IMsgContent;

/**
 *
 * @author Igor
 */
public class VerificationCT implements IMsgContent{
    private int marshalledSize = 4;
    private int errorId = 0;
    private String errorMsg = "default";
    public void setErr(ErrorsEnum eid)
    {
        this.errorId = eid.idx();
        this.errorMsg = eid.getName();
    }
    public int getErr()
    {
        return this.errorId;
    }
    public void setOKCode()
    {
        this.errorId = 0;
    }
    public byte[] marshallToByte()
    {
        ByteBuffer dataArray = ByteBuffer.allocate(128);

	byte [] bytes = new byte[16];
	dataArray.putInt(errorId);
        if(this.errorId != 0)
        {
            System.out.println("[VerificationCT](marshall) - len of Error MSG:" + errorMsg.getBytes());
            dataArray.put(this.errorMsg.getBytes());
            this.marshalledSize = 4 + this.errorMsg.length();
        }
        else
        {
            this.marshalledSize = 4;
        }
	dataArray.rewind();
	
	dataArray.get(bytes,0,bytes.length);
	return bytes;
    }

    public boolean unmarshallFromByte(byte[] content) {
        ByteBuffer dataArray = ByteBuffer.wrap(content);
        if(null == dataArray)
        {
                return false;
        }
        dataArray.clear();
        this.errorId = dataArray.getInt();
        System.out.println("[VerificationCT](unmarshallFromByte) - Error Id:" + this.errorId);
        if(this.errorId != 0)
        {
            try {
                            byte[] cl = new byte[content.length + 1];
                            if(cl[8] != 0)
                            {
                                for(int i = 8; i< content.length; i++)
                                {
                                    cl[i-8] = content[i];
                                }
                                this.errorMsg = new String(cl, "UTF-8");
                            System.out.println("[VerificationCT]- error Msg = AHSTRING:" + errorMsg);
                            }
                    } catch (UnsupportedEncodingException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                    }

            }

        return true;
    }

    public int getSize() {
        return this.marshalledSize;
    }

    public String getConcatContent() {
            String outstr = null;
            outstr = String.valueOf(this.errorId);
            if(this.errorId != 0)
                outstr += this.errorMsg ;
            return outstr;
    }

}
